﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using Server.Services;
using Data.Interfaces;
using System.ServiceModel;
using System.Configuration;
using System.ComponentModel;
using Data;
using Data.Interfaces.Callback;
using System.Threading;
using System.Timers;
using System.Windows.Threading;


namespace Server
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceHost duplexConnectService;
        ServiceHost duplexFightService;
        ServiceHost duplexGameService;
        ServiceHost duplexPlayerService;
        public static DispatcherTimer dispatcherTimer;
        public static List<String> ListPseudo = new List<String>();
        //public static Dictionary<ICFightService, Player> ListPlayers = new Dictionary<ICFightService, Player>();
        public static List<Player> ListPlayers = new List<Player>();
        public static List<ICFightService> listFightCallBacks = new List<ICFightService>();
        public static List<ICGameService> listGameCallBacks = new List<ICGameService>();
        public static List<ICPlayerService> listPlayerCallBacks = new List<ICPlayerService>();
        public static List<ICFightService> listFightCallBacksInactive = new List<ICFightService>();
        public static List<ICGameService> listGameCallBacksInactive = new List<ICGameService>();
        public static List<ICPlayerService> listPlayerCallBacksInactive = new List<ICPlayerService>();
        public static Dictionary<ICPlayerService, Player> callbackPlayerDictionnary = new Dictionary<ICPlayerService, Player>();
       

        public MainWindow()
        {
            InitializeComponent();

            
            
            //ConnectService cs = new ConnectService();
            duplexConnectService = new ServiceHost(typeof(ConnectService));
            duplexConnectService.AddServiceEndpoint(typeof(IConnectService), new NetTcpBinding(SecurityMode.None), String.Format(ConfigurationSettings.AppSettings["ConnectServiceServerAdress"], ConfigurationSettings.AppSettings["adresseIp"]));
            duplexConnectService.Open();

            duplexFightService = new ServiceHost(typeof(FightService));
            duplexFightService.AddServiceEndpoint(typeof(IFightService), new NetTcpBinding(SecurityMode.None), String.Format(ConfigurationSettings.AppSettings["FightServiceServerAdress"], ConfigurationSettings.AppSettings["adresseIp"]));
            duplexFightService.Open();

            duplexGameService = new ServiceHost(typeof(GameService));
            duplexGameService.AddServiceEndpoint(typeof(IGameService), new NetTcpBinding(SecurityMode.None), String.Format(ConfigurationSettings.AppSettings["GameServiceServerAdress"], ConfigurationSettings.AppSettings["adresseIp"]));
            duplexGameService.Open();

            duplexPlayerService = new ServiceHost(typeof(PlayerService));
            duplexPlayerService.AddServiceEndpoint(typeof(IPlayerService), new NetTcpBinding(SecurityMode.None), String.Format(ConfigurationSettings.AppSettings["PlayerServiceServerAdress"], ConfigurationSettings.AppSettings["adresseIp"]));
            duplexPlayerService.Open();

            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500);
            dispatcherTimer.Start();

            /*threadUpdate = new Thread(() => {
                if (MainWindow.GetPlayerCallbacks() != null && MainWindow.GetPlayerCallbacks().Count > 0)
                {
                    foreach (Player player in MainWindow.ListPlayers)
                    {
                        foreach (ICPlayerService callback in MainWindow.GetPlayerCallbacks())
                        {
                            //OperationContext.Current.GetCallbackChannel<ICPlayerService>().updateOtherPlayer(pseudo, classePerso, posX, posY, orientation, step, life, mana);

                            callback.updateOtherPlayer(player.pseudo, player.character.name, player.posX, player.posY, player.orientation, player.step, player.character.life, player.character.mana);
                            
                        }
                    }
                }
            });*/
        }
        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            Refresh();
        }
       

        private void Refresh()
        {
            LabelConnectedPlayer.Content  = "Player(s) Connected : "+listGameCallBacks.Count;
            ListPlayerCo.Items.Clear();
              
                foreach (Player player in MainWindow.ListPlayers)
                {
                    
                 
                    ListPlayerCo.Items.Add(player.pseudo + " ("+player.character.name+")");

                }
            
        }

        public void button1_Click(object sender, RoutedEventArgs e)
        {
            Refresh();
        }

        public static List<ICFightService> GetFightCallbacks()
        {
            List<ICFightService> listCallBackActive = new List<ICFightService>();
            foreach (ICFightService cb in listFightCallBacks)
            {
                if (!listFightCallBacksInactive.Contains(cb))
                {
                    listCallBackActive.Add(cb);
                }
            }
            return listCallBackActive;
        }

        public static List<ICGameService> GetGameCallbacks()
        {
            List<ICGameService> listCallBackActive = new List<ICGameService>();
            foreach (ICGameService cb in listGameCallBacks)
            {
                if (!listGameCallBacksInactive.Contains(cb))
                {
                    listCallBackActive.Add(cb);
                }
            }
            return listCallBackActive;
        }

        public static List<ICPlayerService> GetPlayerCallbacks()
        {
            List<ICPlayerService> listCallBackActive = new List<ICPlayerService>();
            foreach (ICPlayerService cb in listPlayerCallBacks)
            {
                if (!listPlayerCallBacksInactive.Contains(cb))
                {
                    listCallBackActive.Add(cb);
                }
            }
            return listCallBackActive;
        }

        //clic sur bouton start
        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            dispatcherTimer.Stop();
            foreach (ICGameService cb in GetGameCallbacks())
            {
                cb.startGame();
                
            }
            Thread test = new Thread(() => {
                while (true)
                {
                    if (MainWindow.GetPlayerCallbacks() != null && MainWindow.GetPlayerCallbacks().Count > 0)
                    {
                        foreach (Player player in MainWindow.ListPlayers)
                        {
                            
                            foreach (ICPlayerService callback in MainWindow.GetPlayerCallbacks())
                            {
                                //OperationContext.Current.GetCallbackChannel<ICPlayerService>().updateOtherPlayer(pseudo, classePerso, posX, posY, orientation, step, life, mana);
                                try
                                {
                                    bool pushData = true;
                                    if (MainWindow.callbackPlayerDictionnary.ContainsKey(callback) && MainWindow.callbackPlayerDictionnary[callback].pseudo == player.pseudo)
                                        pushData = false;
                                    if(pushData)
                                        callback.updateOtherPlayer(player.pseudo, player.character.name, player.posX, player.posY, player.orientation, player.step, player.character.life, player.character.mana);
                                }
                                catch(Exception){}
                            }
                        }
                    }
                    Thread.Sleep(10);
                }
            });
            test.Start();


            Thread regenThread = new Thread(() => {
                while (true)
                {
                    if (MainWindow.GetPlayerCallbacks() != null && MainWindow.GetPlayerCallbacks().Count > 0)
                    {
                        foreach (ICPlayerService callback in MainWindow.GetPlayerCallbacks())
                        {
                            callback.regenStats();
                        }
                    }
                    Thread.Sleep(3000);
                }
            });
            regenThread.Start();
            
        }

        

    

    }
}
